package Entity;
import java.util.Date;
import java.io.Serializable;
import Controller.*; 

/**
 * This class is for making new complaints by the customers. 
 * Every customer can complaint about the service.
 * The customer service worker will have to answer to this complaint within 24 hr.
 * The customer can be compensated or not, it is the customer service worker's choice.
 * This entity will be used by the customer (for making the complaint) and the customer service worker (for handling it).
 * 
 * @author Moran Abilea
 *
 */
public class Complaint implements Serializable
{
	private static final long serialVersionUID = 5779351855427869637L;
	
	/**
	 * is a static integer for setting "complaint time stamp" unique to each customer complaint. 
	 */
	private static int complaintStamp=0; // this is a var for setting "time stamp" to the complaint number.
	/**
	 * is an enum for knowing the errors in case of invalid input.
	 * IDOCCERR- means an error in entering the id (not entering 9 digits).
	 * DESCERR- means an error in description of the complaint (the description is empty). 
	 * RESERR- means an error in result of the complaint (the description of the answer is empty).
	 * COMPERR- means an error in the compensation (the compensation <0)
	 */
	public enum validateErrs{IDERR,DESCERR,RESERR,COMPERR}; // this is an enum for setting errors for the validate function.
	
	// The attributes that are relevant for the customer.
	private int complaint_number; // this is the number of the complaint of a specific customer.
	private String customer_id; // this is the customer id of the complainer.
	private Date dateOfsubmission; // this is a date of the complaint submission.
	private String description; // this is a string for saving the customer complaint description.
	// end of attributes for customer.
	
	private boolean isHandled; // this flag is for knowing if the complaint was handled.
	
	// The attributes that are relevant for the worker (besides the above).
	private String worker_id;
	private String result; // this is a string for saving the result of the complaint.
	private Date dateOfAnswer; // this is a date of the complaint answering.
	private double compensation; // this is the compensation for the customer (if exist).
	// end of attributes for worker.
	
	/**
	 * is the constructor that will be used by the customer for creating a new complaint.
	 * it will create the new time stamp for the complaint, set the submission date and get the customer's input.
	 * @param cid = the customer id.
	 * @param desc = the description of the customer.
	 */
	//  this will be a constructor which is relevant only to customer.
	public Complaint(String cid,String desc){
		dateOfsubmission=new Date(); // the date of submission will be set to today's date.
		complaintStamp++; // our "time stamp" for the compliant_number will increase.
		this.complaint_number=complaintStamp; // set the complaint_number as the complaint stamp.
		// copy the parameters as it.
		this.customer_id=cid;
		this.description=desc;
		isHandled=false; // the flag will be default as false.
	}
	/**
	 * is the constructor that will be used by the customer service worker for answering the complaint.
	 * it will create the date of answering and get the customer service worker's input.
	 * @param wid = the worker id that will answer the complaint (in case we will have more that one worker that answer it).
	 * @param result = the answer of the worker.
	 * @param compensation = the compensation for the customer (if he deserved one).
	 */
	// this will be constructor which is relevant to worker for answering the relevant complaint.
	public Complaint(String wid,String result, double compensation){
		dateOfAnswer=new Date(); // the date of answering will be set to today's date.
		// copy the parameters as it.
		this.worker_id=wid;
		this.result=result;
		this.compensation=compensation;
	}
	
	// the get functions.
	public int getComplaint_number() { return complaint_number; }
	public String getCustomer_id() { return customer_id; }
	public Date getDateOfSubmition() { return dateOfsubmission; }
	public String getDescription() { return description; }
	public static int getComplaintStamp() { return complaintStamp; }
	public String getResult() { return result; }
	public boolean isHandled() { return isHandled; }
	public double getCompensation() {return compensation;}
	public Date getDateOfAnswer() {return dateOfAnswer;}
	public Date getDateOfsubmission() {return dateOfsubmission;}
	public String getWorker_id() {return worker_id;}
	
	// the set functions.
	public void setCustomer_id(String customer_id) { this.customer_id = customer_id; }
	public void setDescription(String description) { this.description = description; }
	public void setHandled(boolean isHandled) { this.isHandled = isHandled; }
	public void setResult(String result) { this.result = result; }
	public void setCompensation(double compensation) {this.compensation = compensation;}
	public void setWorker_id(String worker_id) {this.worker_id = worker_id;}
	public void setDateOfAnswer(Date dateOfAnswer) {
		this.dateOfAnswer = dateOfAnswer;
	}
	public void setComplaint_number(int complaint_number) {
		this.complaint_number = complaint_number;
	}
	public void setDateOfsubmission(Date dateOfsubmission) {
		this.dateOfsubmission = dateOfsubmission;
	}
	
	/**
	 * is the function for validate the input from the customer.
	 * @return which error occurs.(IDERR for error in the customer id  or DESCERR for error in an empty description).
	 */
	// this function is for validating the vars for the customer.
	public validateErrs cvalidate()
	{
		if(this.customer_id.length()!=9)
			return validateErrs.IDERR;
		if(this.description.equals(" ")&&this.customer_id.length()==9)
			return validateErrs.DESCERR;
		return null;
	}
	
	/**
	 * is the function for validate the input from the customer service worker.
	 * @return which error occurs.(IDERR for error in the id, RESS for error in an empty answer or COMPERR for error in the compensation).
	 */
	// this function is for validating the vars for the worker.
	public validateErrs wvalidate()
	{
		if(this.worker_id.length()!=9)
			return validateErrs.IDERR;
		if(this.result.equals(" ")&&this.worker_id.length()==9)
			return validateErrs.RESERR;
		if(this.compensation<0)
			return validateErrs.COMPERR;
		return null;
	}
	
	/**
	 * is the fucntion for saving the new complaint to DB (if it is a new one by the customer or answered one by the customer service worker).
	 */
	// this function is for saving complaint to the DB.
	public void save()
	{
		Complaint c=new Complaint(this.customer_id,this.description);

		DBcontroller.saveComplaintToDB(c);
	}
	
	
}